package cn.smile.controller.admin;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smile.bean.PageBean;
import cn.smile.bean.Result;
import cn.smile.entity.common.Comment;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.Student;
import cn.smile.service.common.CommentService;
import cn.smile.service.common.GoodsService;
import cn.smile.service.common.StudentService;

import javax.annotation.Resource;

/**
 * 后台物品评论管理控制器
 */
@RequestMapping("/admin/comment")
@Controller
public class CommentController {

    @Resource
    private GoodsService goodsService;
    @Resource
    private StudentService studentService;
    @Resource
    private CommentService commentService;

    /**
     * 物品评论管理列表页面
     */
    @SuppressWarnings("DuplicatedCode")
    @RequestMapping(value = "/list")
    public String list(Comment comment, PageBean<Comment> pageBean, Model model) {
        if (comment.getStudent() != null && comment.getStudent().getSn() != null) {
            Student student = studentService.findBySn(comment.getStudent().getSn());
            if (student != null) {
                comment.setStudent(student);
            }
        }
        List<Goods> goodsList = null;
        if (comment.getGoods() != null && comment.getGoods().getName() != null) {
            goodsList = goodsService.findListByName(comment.getGoods().getName());
        }
        model.addAttribute("title", "物品评论列表");
        model.addAttribute("content", comment.getContent());
        model.addAttribute("name", comment.getGoods() == null ? null : comment.getGoods().getName());
        model.addAttribute("sn", comment.getStudent() == null ? null : comment.getStudent().getSn());
        model.addAttribute("pageBean", commentService.findlist(pageBean, comment, goodsList));
        return "admin/comment/list";
    }


    /**
     * 物品评论删除操作
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        commentService.delete(id);
        return Result.success(true);
    }
}
